package com.peanut.devlibrary.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.peanut.devlibrary.LibContext;

/**
 * Created by anonymouslystudio on 2017/7/13.
 */

public class SysUtil {
    public static int dip2px(float dpValue) {
        final float scale = LibContext.getInstance().getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(float pxValue) {
        final float scale = LibContext.getInstance().getContext().getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int sp2px(float spValue) {
        final float fontScale = LibContext.getInstance().getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
    public static void contact(Context ctx, String mobile) {
        try {
            Uri uri = Uri.parse("tel:" + mobile + "");
            Intent i = new Intent(Intent.ACTION_DIAL, uri);
            ctx.startActivity(i);
        } catch (Exception e) {
            //on some devices, there is no phone functions, such as some tablet
            Toast.makeText(ctx, "没有找到电话应用", Toast.LENGTH_SHORT).show();
        }
    }



}
